package com.example.demo.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.system.entity.DictItem;
import com.example.demo.system.entity.DictType;
import com.example.demo.system.service.DictService;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
@Tag(name = "dict", description = "数据字典管理")
public class DictController {

    @Autowired
    private DictService iDictService;

    // ----------------- 字典类型管理 -----------------
    @GetMapping("/types/page")
    @Operation(summary = "分页查询字典类型", description = "分页查询字典类型")
    public Result<Page<DictType>> getDictTypePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String typeName
            ) {
        Page<DictType> page = new Page<>(pageNum, pageSize);
        return Result.ok(iDictService.getDictTypePage(page,typeName));
    }

    @PostMapping("/types")
    @Operation(summary = "新增字典类型", description = "新增字典类型")
    public Result<Void> saveDictType(@RequestBody DictType dictType) {
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_code", dictType.getTypeCode());
        if (iDictService.count(queryWrapper) > 0) {
            return Result.error(new Exception("字典类型已存在"));
        }
        iDictService.saveDictType(dictType);
        return Result.ok();
    }

    @PutMapping("/types")
    @Operation(summary = "修改字典类型", description = "修改字典类型")
    public Result<Void> updateDictType(@RequestBody DictType dictType) {
        iDictService.updateDictType(dictType);
        return Result.ok();
    }

    @DeleteMapping("/types/{id}")
    @Operation(summary = "删除字典类型", description = "删除字典类型")
    public Result<Void> deleteDictType(@PathVariable Long id) {
        iDictService.deleteDictType(id);
        return Result.ok();
    }

    @DeleteMapping("/types/batch/{ids}")
    @Operation(summary = "批量删除字典类型", description = "批量删除字典类型")
    public Result<Void> deleteDictType(@PathVariable String ids) {
        List<String> idArray = Arrays.asList(ids.split(","));
        iDictService.removeBatchByIds(idArray);
        return Result.ok();
    }

    // ----------------- 字典项管理 -----------------
    @GetMapping("/items/{typeCode}")
    @Operation(summary = "按类型查询字典项列表", description = "按类型查询字典项列表")
    public Result<List<DictItem>> getItemsByType(@PathVariable String typeCode) {
        return Result.ok(iDictService.getDictItemsByType(typeCode));
    }

    @GetMapping("/items/map/{typeCode}")
    @Operation(summary = "按类型获取字典键值映射", description = "按类型获取字典键值映射")
    public Result<Map<String, String>> getDictMap(@PathVariable String typeCode) {
        return Result.ok(iDictService.getDictMapByType(typeCode));
    }

    @PostMapping("/items")
    @Operation(summary = "新增字典项", description = "新增字典项")
    public Result<Void> saveDictItem(@RequestBody DictItem dictItem) {
        iDictService.saveDictItem(dictItem);
        return Result.ok();
    }

    @PutMapping("/items")
    @Operation(summary = "修改字典项", description = "修改字典项")
    public Result<Void> updateDictItem(@RequestBody DictItem dictItem) {
        iDictService.updateDictItem(dictItem);
        return Result.ok();
    }

    @DeleteMapping("/items/{id}")
    @Operation(summary = "删除字典项", description = "删除字典项")
    public Result<Void> deleteDictItem(@PathVariable Long id) {
        iDictService.deleteDictItem(id);
        return Result.ok();
    }

    // ----------------- 缓存管理 -----------------
    @PostMapping("/cache/refresh")
    @Operation(summary = "刷新字典缓存", description = "刷新字典缓存")
    public Result<Void> refreshDictCache() {
        iDictService.refreshDictCache();
        return Result.ok();
    }
}
